package bogdan.trainee
import java.util.Scanner;
/**
 * @author Olexandr Bogdan
 * 
 * Program search substring - palindromes in the input string and displays them on the screen.
 */
public class Palindrom {

	public static void main(String[] args) {
	
		print(findPalindrome(inputString()));	
	
	}
	
	/**
	 * 
	 * @return String - input text
	 * Method initializes the input device, reads characters from the console  and returns it to a string
	 */
	private static String inputString(){
		Scanner scan = new Scanner(System.in);
		System.out.print("Введіть текст : ");
		String text = scan.nextLine();
		scan.close();
		return text;
	}
	
	/** 
	 * @param  text - text to check for broadly, the presence of palindromes
	 * @return  an array of strings - palindromes
	 * Method in the input string search substring - palindromes and returns an array of strings - palindromes
	 */
	private static String[] findPalindrome(String text) {
		int delta;
		boolean isPalindrom = false;
		StringBuffer allPalindromes = new StringBuffer();
		
		for(int indexOfFirstSymbol = 0 ; indexOfFirstSymbol < text.length() ; indexOfFirstSymbol++){
			for(int indexOfLastSymbol = text.length() - 1 ; indexOfLastSymbol > indexOfFirstSymbol ; indexOfLastSymbol--){
				if((text.codePointAt(indexOfFirstSymbol) == text.codePointAt(indexOfLastSymbol)) & (indexOfLastSymbol - indexOfFirstSymbol > 1) ){
					delta = 0;
					isPalindrom = true;
					while(indexOfLastSymbol-delta >= indexOfFirstSymbol+delta){
						if(text.codePointAt(indexOfFirstSymbol + delta) == text.codePointAt(indexOfLastSymbol - delta)){
						delta++;	
						}else{
							isPalindrom = false;
							break;
						}
					}
					if(isPalindrom){
						allPalindromes.append(text.substring(indexOfFirstSymbol, indexOfLastSymbol+1)).append('`');
					}				
				}
			}
		}
		return allPalindromes.toString().split("`");
	}
	
	/**
	 * @param array of string - an array of strings - palindromes
	 * Method displays strings on the screen
	 */
	private static void print(String... args) {
		if(args[0].length() > 0){
			for(String s : args){
				System.out.print("паліндром : ");
				System.out.println(s);
			}
		}else{
			System.out.println("Паліндроми відсутні");
		}
		
	}
}
